//
//  StateViewer.m
//  YuchBox
//
//  Created by Ivan on 11-11-11.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "StateViewer.h"
#import "YuchBoxAppDelegate.h"

@implementation StateViewer
//SYNTHESIZE_SINGLETON_FOR_CLASS(StateViewer);
@synthesize button              = _button;
@synthesize currentStateEvent   = _currentStateEvent;

- (id)initSingleton
{
    self = [self initWithOrigin:CGPointMake(0, 0)];
    return self;
}

- (void)touchDown:(UIButton *)button
{
    _timeInterval = [[NSDate dateWithTimeIntervalSinceNow:0] timeIntervalSince1970];
}

- (void)touchUpInside:(UIButton *)button
{
    if (([[NSDate dateWithTimeIntervalSinceNow:0] timeIntervalSince1970] -  _timeInterval) > 1.2f) {
//        [[YuchBoxAppDelegate shareAppDelegate] setupLoginView];
    }
}

- (id)initWithOrigin:(CGPoint)origin
{
    self = [super initWithFrame:CGRectMake(origin.x, origin.y, 32, 32)];
    if (self) {
        _stateImages = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"main.png"],
                        [UIImage imageNamed:@"main_new.png"],
                        [UIImage imageNamed:@"main_offline.png"],
                        [UIImage imageNamed:@"main_offline_new.png"], nil];
        
        //_button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 32, 32)]
        _currentStateEvent = StateEventNone;
        _currentStateEvent = StateMessageNone;
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.adjustsImageWhenHighlighted = NO;
        button.frame = CGRectMake(0, 0, 32, 32);
        [button addTarget:self action:@selector(touchDown:)      forControlEvents:UIControlEventTouchDown];
        [button addTarget:self action:@selector(touchUpInside:)  forControlEvents:UIControlEventTouchUpInside];
//        [button addTarget:self action:@selector(touchUpOutside:) forControlEvents:UIControlEventTouchUpOutside];

        [self setButton:button];
        [self addSubview:_button];
        
        [self setTemporaryHide:YES delegate:nil];
        [self setStateEvent:StateEventOffline];
        [self setStateMessage:StateMessageNoNewMessage];
//        [self setState:StateEventOffline animated:NO delegate:nil];
    }
    return self;
}

- (void)setOrigin:(CGPoint)origin delegate:(NSObject<StateViewerDelegate> *)delegate
{
    self.frame = CGRectMake(origin.x, origin.y, 32, 32);
}

- (void)setStateEvent:(StateEvent)stateEvent
{
    if (_currentStateEvent != stateEvent) {
        [self setStateImageIndex:(stateEvent + _currentStateMessage) animated:NO];
        _currentStateEvent = stateEvent;
    }
}

- (void)setStateMessage:(StateMessage)stateMessage
{
    if (_currentStateMessage != stateMessage) {
        [self setStateImageIndex:(_currentStateEvent + stateMessage) animated:NO];
        _currentStateMessage = stateMessage;
    }
}

- (void)setStateImageIndex:(StateImageIndex)stateImageIndex animated:(Boolean)animated
{
    if (animated)
    {
        [UIView beginAnimations:@"animated" context:nil];
        [UIView setAnimationDuration:0.8f];
        [UIView setAnimationRepeatCount:2];
        
        [_button setAlpha:0.1f];
        [_button setBackgroundImage:[_stateImages objectAtIndex:stateImageIndex] forState:UIControlStateNormal];
        [_button setAlpha:1];
        [UIView commitAnimations];
        
    } else {
        [_button setBackgroundImage:[_stateImages objectAtIndex:stateImageIndex] forState:UIControlStateNormal];
    }
}

//- (void)setState:(StateEvent)state animated:(Boolean)animated delegate:(NSObject<StateViewerDelegate> *)delegate
//{
//    if (_currentStateEvent != state) {
//
//        
//        _currentStateEvent = state;
//    }
//}

- (void)setTemporaryHide:(Boolean)isHidden delegate:(NSObject<StateViewerDelegate> *)delegate
{
//    if (!isHidden) {
    [UIView beginAnimations:@"animationID1" context:nil];
    [UIView setAnimationDelay:(isHidden ? 0.01f : 0.01f)];
        
    self.alpha = isHidden ? 0.0f : 1.0f;
        
        //[delegate stateViewerSetup:self];
    [UIView commitAnimations];
//    } else {
        //[delegate stateViewerUnsetup:self];
//    }
}

- (void)dealloc
{
    [_imageView release];
    [_button release];
    
    [_stateImages release];
    [super dealloc];
}

@end
